/*
 * 文件名： GearStateTag.java
 * 
 * 创建日期： 2016年11月29日
 *
 * Copyright(C) 2016, by <a href="mailto:liws@xingyuanauto.com">liws</a>.
 *
 * 原始作者: liws
 *
 */
package com.moli.iov.protocol.enums.realtime;

import java.util.Arrays;

import com.moli.iov.protocol.enums.IDictsEnumTag;
import com.moli.iov.util.ByteArrayUtil;


/**
 * 车辆档位状态
 *
 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
 *
 * @version $Revision$
 *
 * @since 2016年11月29日
 */
public class GearStateTag implements IDictsEnumTag {

	 /** 
	 */ 
	private static final long serialVersionUID = 1L;

	// 二进制0
	private static final String BIT_0 = "0";

	// 二进制1
	private static final String BIT_1 = "1";

	private static final int MAX_INDEX = 7;

	// 存放单字节8位
	private String[] gearsBytes =
		{ BIT_0, BIT_0, BIT_0, BIT_0, BIT_0, BIT_0, BIT_0, BIT_0 };
	public GearStateTag() {
	}
	public GearStateTag(ExistDriveForces existDF, ExistBraking existB, Gears gears) {
		setExistDriveForces(existDF);
		setExistBraking(existB);
		setGear(gears);
	}

	private String k;

	private byte v;

	public void setGearState(ExistDriveForces existDF, ExistBraking existB, Gears gears) {

		setExistDriveForces(existDF);
		setExistBraking(existB);
		setGear(gears);
	}

	/**
	 * 设置 档位信息 bit低4位
	 * 
	 * @param yes_no
	 */
	public void setGear(Gears gears) {

		gearsBytes[MAX_INDEX - 0] = String.valueOf(gears.getValue().charAt(3));
		gearsBytes[MAX_INDEX - 1] = String.valueOf(gears.getValue().charAt(2));
		gearsBytes[MAX_INDEX - 2] = String.valueOf(gears.getValue().charAt(1));
		gearsBytes[MAX_INDEX - 3] = String.valueOf(gears.getValue().charAt(0));
	}
	/**
	 * 获取当前档位
	 */
	public Gears getGear(){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(gearsBytes[MAX_INDEX - 3]);
		sBuilder.append(gearsBytes[MAX_INDEX - 2]);
		sBuilder.append(gearsBytes[MAX_INDEX - 1]);
		sBuilder.append(gearsBytes[MAX_INDEX - 0]);
		return Gears.valOf(sBuilder.toString());
	}
	/**
	 * 设置 是否有驱动力
	 * 
	 * @param edf
	 */
	public void setExistDriveForces(ExistDriveForces edf) {
		gearsBytes[MAX_INDEX - 5] = edf.getValue();
	}
	/**
	 * 获取是否有驱动力
	 * 
	 * @return boolean
	 *   true 有驱动力
	 *   false 无驱动力
	 */
	public ExistDriveForces getExistDriveForces(){
		return ExistDriveForces.valOf(gearsBytes[MAX_INDEX - 5]);
	}
	/**
	 * 设置 是否有制动力
	 * 
	 * @param ebk
	 */
	public void setExistBraking(ExistBraking ebk) {
		gearsBytes[MAX_INDEX - 4] = ebk.getValue();
	}
	/**
	 * 获取是否有制动力
	 * 
	 * @return boolean
	 *   true 有制动力
	 *   false 无制动力
	 */
	public ExistBraking getExstBreaking(){
		return ExistBraking.valOf(gearsBytes[MAX_INDEX - 4]);
	}
	/**
	 *
	 */
	@Override
	public byte getV() {
		StringBuilder bitsBuilder = new StringBuilder();
		for (String b : gearsBytes) {
			bitsBuilder.append(b);
		}
		this.v = ByteArrayUtil.bitToByte(bitsBuilder.toString());
		return this.v;
	}

	/**
	 *
	 */
	@Override
	public String getK() {
		StringBuilder sBuilder = new StringBuilder();
		StringBuilder gearBuilder = new StringBuilder();
		gearBuilder.append(gearsBytes[4] + gearsBytes[5] + gearsBytes[6] + gearsBytes[7]);
		sBuilder.append(ExistDriveForces.getKey(gearsBytes[2]) + "-" + ExistBraking.getKey(gearsBytes[3]) + "-"
				+ Gears.getKey(gearBuilder.toString()));
		this.k = sBuilder.toString();
		return this.k;
	}

	/**
	 * bit低4位 档位枚举
	 *
	 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
	 *
	 * @version $Revision$
	 *
	 * @since 2016年11月29日
	 */
	public enum Gears {
		NEUTRAL("空挡", "0000"), G1("1挡", "0001"), G2("2挡", "0010"), G3("3挡", "0011"), G4("4挡", "0100"), G5("5挡",
				"0101"), G6("6挡", "0110"), G7("7挡", "0111"), G8("8挡", "1000"), G9("9挡", "1001"), G10("10挡",
						"1010"), G11("11挡", "1011"), G12("12挡",
								"1100"), REVERSE("倒挡", "1101"), DRIVE("自动D挡", "1110"), PARK("停车P挡", "1111");

		private String key;

		private String value;

		Gears(String key, String value) {
			this.key = key;
			this.value = value;
		}

		public static Gears valOf(String value) {
			if ("0000".equals(value)) {
				return NEUTRAL;
			}
			else if ("0001".equals(value)) {
				return G1;
			}
			else if ("0010".equals(value)) {
				return G2;
			}
			else if ("0011".equals(value)) {
				return G3;
			}
			else if ("0100".equals(value)) {
				return G4;
			}
			else if ("0101".equals(value)) {
				return G5;
			}
			else if ("0110".equals(value)) {
				return G6;
			}
			else if ("0111".equals(value)) {
				return G7;
			}
			else if ("1000".equals(value)) {
				return G8;
			}
			else if ("1001".equals(value)) {
				return G9;
			}
			else if ("1010".equals(value)) {
				return G10;
			}
			else if ("1011".equals(value)) {
				return G11;
			}
			else if ("1100".equals(value)) {
				return G12;
			}
			else if ("1101".equals(value)) {
				return REVERSE;
			}
			else if ("1110".equals(value)) {
				return DRIVE;
			}
			else if ("1111".equals(value)) {
				return PARK;
			}
			return null;
		}

		public static String getKey(String value) {
			for (Gears c : Gears.values()) {
				if (c.getValue().equals(value)) {
					return c.key;
				}
			}
			return null;
		}

		public String getKey() {
			return this.key;
		}

		public String getValue() {
			return this.value;
		}
	}
	/**
	 * 是否存在制动力 枚举
	 *
	 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
	 *
	 * @version $Revision$
	 *
	 * @since 2016年11月29日
	 */
	public enum ExistBraking {
		YES("有制动力", "1"), NO("无制动力", "0");
		private String key;

		private String value;

		ExistBraking(String key, String value) {
			this.key = key;
			this.value = value;
		}

		public static ExistBraking valOf(String value) {
			if ("1".equals(value)) {
				return YES;
			}
			else if ("0".equals(value)) {
				return NO;
			}
			return null;
		}

		public static String getKey(String value) {
			for (ExistBraking c : ExistBraking.values()) {
				if (c.getValue().equals(value)) {
					return c.key;
				}
			}
			return null;
		}

		public String getKey() {
			return this.key;
		}

		public String getValue() {
			return this.value;
		}
	}
	/**
	 * 是否存在驱动力 枚举
	 *
	 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
	 *
	 * @version $Revision$
	 *
	 * @since 2016年11月29日
	 */
	public enum ExistDriveForces {
		YES("有驱动力", "1"), NO("无驱动力", "0");
		private String key;

		private String value;

		ExistDriveForces(String key, String value) {
			this.key = key;
			this.value = value;
		}

		public static ExistDriveForces valOf(String value) {
			if ("1".equals(value)) {
				return YES;
			}
			else if ("0".equals(value)) {
				return NO;
			}
			return null;
		}

		public static String getKey(String value) {
			for (ExistDriveForces c : ExistDriveForces.values()) {
				if (c.getValue().equals(value)) {
					return c.key;
				}
			}
			return null;
		}

		public String getKey() {
			return this.key;
		}

		public String getValue() {
			return this.value;
		}
	}

	public static void main(String[] args) {
		GearStateTag locationStateTag = new GearStateTag();
		locationStateTag.setGear(Gears.REVERSE);
		locationStateTag.setExistBraking(ExistBraking.NO);
		locationStateTag.setExistDriveForces(ExistDriveForces.NO);
	}
	 /**
	 *
	 */
	@Override
	public IDictsEnumTag valueOf(byte pByte) {
		String bits = ByteArrayUtil.byteToBit(pByte);
		char [] charGearTag = bits.toCharArray();
		int length = charGearTag.length;
		char [] chageG = Arrays.copyOfRange(charGearTag, length-4, length);
		char [] chageEbk = Arrays.copyOfRange(charGearTag, length-5, length-4);
		char [] chageEdf = Arrays.copyOfRange(charGearTag, bits.length()-6, length-5);
		GearStateTag gStateTag = new GearStateTag();
		gStateTag.setGear(Gears.valOf(new String(chageG)));
		gStateTag.setExistBraking(ExistBraking.valOf(new String(chageEbk)));
		gStateTag.setExistDriveForces(ExistDriveForces.valOf(new String(chageEdf)));
		return gStateTag;
	}
}
